package com.cn.esermis.datawarn.common;

import com.baomidou.dynamic.datasource.annotation.DS;

import com.cn.esermis.datawarn.mapper.CountAirChargeMapper;
import com.cn.esermis.datawarn.mapper.CountWaterChargeMapper;
import com.cn.esermis.datawarn.mapper.ExceedDataMapper;
import com.cn.esermis.datawarn.mapper.MonitorFactorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

/**
 * @ClassName: exceptionDataDispose
 * @author: Mr.Chen
 * @date: 2024/9/13 9:51
 * 正常"0" 异常"1"
 */
@DS("slave_1")
@Component
@EnableScheduling
public class ExceptionDataDispose {

    @Autowired
    CountAirChargeMapper countAirChargeMapper;
    @Autowired
    ExceedDataMapper exceedDataMapper;
    @Autowired
    MonitorFactorMapper monitorFactorMapper;
    @Autowired
    CountWaterChargeMapper countWaterChargeMapper;


    public String LoseData(Double min) {
        System.out.println("收到最小值-->min" + min);
        //判断是否为负值数据
        if (min < 0) {
            return "1";
        }
        return "0";
    }

    public static Date getSqlDate(Date date) {
        return new Date(date.getTime());
    }

    public Date getPreviousDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, -1);
        return getSqlDate(calendar.getTime());
    }

    public String SixData(String flag, String mn, String factor, Double avg, String dataTime) {
        //判断是否为六小时恒值数据
        int count = 1;
        List<String> dataAll = BackDate(dataTime);
        List<BigDecimal> all = new ArrayList<>();
        String zd = "avg";
        if (flag.equals("1")) {
            zd = "zs_avg";
        }
        System.out.println("使用值字段为--> " + zd);
        if (factor.contains("a")) {
            all = countAirChargeMapper.airSixHour(zd, mn, factor, dataAll.get(0),
                    dataAll.get(1), dataAll.get(2), dataAll.get(3), dataAll.get(4));
            System.out.println("废气收到参数-->flag = " + flag + ";mn = " + mn + ";factor =" + factor + ";avg = " + avg + "dataTime = " + dataTime);
        }
        if (factor.contains("w")) {
            all = countWaterChargeMapper.airSixHour(zd, mn, factor, dataAll.get(0),
                    dataAll.get(1), dataAll.get(2), dataAll.get(3), dataAll.get(4));
            System.out.println("废水收到参数-->flag = " + flag + ";mn = " + mn + ";factor =" + factor + ";avg = " + avg + "dataTime = " + dataTime);
        }
        System.out.println("进入是否为恒值判断");
        for (BigDecimal bigDecimal : all) {
            int i = 1;
            if (Objects.nonNull(bigDecimal)) {
                i = bigDecimal.compareTo(BigDecimal.valueOf(avg));
                if (i == 0) {
                    count++;
                }
            }
        }
        System.out.println("判断完成");
        if (count == 6) {
            System.out.println("检测完成--> 恒值状态为" + "1");
            return "1";
        }
        System.out.println("检测完成--> 恒值状态为" + "0");
        return "0";
    }

    public List<String> BackDate(String data) {
        String dataTime = data;
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            StringBuilder stringBuilder = new StringBuilder(dataTime);
            //2024-11-28 11:00:00
            stringBuilder.insert(4, "-").insert(7, "-").insert(10, " ").insert(13, ":").insert(16, ":");
            String da = DateUtil.formateDateTime(getPreviousDate(DateUtil.parseDateTime(stringBuilder.toString())));
//            String da = DateUtil.formateDateTime(getPreviousDate(DateUtil.parseDateTime(stringBuilder.toString())));
            dataTime = da.replace("-", "").replace(" ", "").replace(":", "");
            arrayList.add(dataTime);
        }
        System.out.println("获取时间为-->" + arrayList.toString());
        return arrayList;
    }

//    @Scheduled(cron = "0/10 * * * *  ?")
    public void test() {
        String dataTime = "20241204040000";
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            StringBuilder stringBuilder = new StringBuilder(dataTime);
            //2024-11-28 11:00:00
            stringBuilder.insert(4, "-").insert(7, "-").insert(10, " ").insert(13, ":").insert(16, ":");
            String da = DateUtil.formateDateTime(getPreviousDate(DateUtil.parseDateTime(stringBuilder.toString())));
            dataTime = da.replace("-", "").replace(" ", "").replace(":", "");
            arrayList.add(dataTime);
        }
        System.out.println(arrayList.toString());
        System.out.println(SixData("0", "3503054411032D", "a21002", 0.0, "20241205140000"));
//        System.out.println(" mn --> 3503054411032D \n factor --> a21002 \n avg --> 0.0  \n 返回值 -->" + ExceedData("3503054411032D","a21005",0.0));
//        System.out.println(" mn --> 3503054411032D \n factor --> a21005 \n avg --> 0.0  \n 返回值 -->" + ExceedData("3503054411032D","a21005",0.0));
    }

    public String ExceedData(String mn, String factor, Double avg) {
        //获取到小时均值限值
        String hoursLimit = ExceedData(mn, factor);
        double hl = 0.0;
        double ll = 0.0;
        System.out.println("收到参数--> mn = " + mn + ";factor =" + factor + ";avg = " + avg);
        if (Objects.isNull(hoursLimit)) {
            System.out.println("未取得限值信息");
            return "0";
        }
        if (hoursLimit.contains("-")) {
            hl = Double.parseDouble(hoursLimit.substring(hoursLimit.indexOf("-") + 1));
            ll = Double.parseDouble(hoursLimit.substring(0, hoursLimit.indexOf("-")));
            System.out.println("取得限值信息为区间值-->最大" + hl + ";最小" + ll);
            if (hl < avg || ll > avg) {
                return "1";
            } else {
                return "0";
            }
        } else {
            System.out.println("取得限值信息-->" + hoursLimit);
            hl = Double.parseDouble(hoursLimit);
        }
        if (hl < avg) {
            return "1";
        }
        return "0";
    }

    public String ExceedData(String mn, String factor) {
        return exceedDataMapper.hoursAvg(mn, factor);
    }

}
